package CSS::Parse::AsBuilder;

$VERSION = 0.01;

use strict;
use warnings;

use Carp;

use base 'CSS::Parse';
use CSS::Builder::Property;

use CSS::Parse::AsBuilder::Selectors;
use CSS::Parse::AsBuilder::Styles;

# The main parser
sub parse_string {
      my $self = shift;
      my $string = shift;

      $string =~ s/\r\n|\r|\n/ /g;
      
      my @list = ();
      # Split into styles
      foreach ( grep { /\S/ } split /(?<=\})/, $string ) {
	    unless ( /^\s*([^{]+?)\s*\{(.*)\}\s*$/ ) {
		  croak( "Invalid or unexpected style data '$_'" );
	    }
	    push @list , $self->create_style($1, $2);
      }
      return @list;
}

# add a style to the style list
sub create_style {
      my ($self , $selectors , $styles) = @_;
      
      # new propery containing name and styles
      my $property = CSS::Builder::Property->new();
      
      # build list of selectors
      my $sel = CSS::Parse::AsBuilder::Selectors->new();
      my $sel_res = $sel->build_selectors($selectors);
      $property->selector_push(@$sel_res);
      
      # build list of contents
      my $sty = CSS::Parse::AsBuilder::Styles->new();
      my $sty_res = $sty->build_styles($styles);
      $property->list_push(@$sty_res);
      
      # return property
      return $property;
}

1;